// 工具函数包 utils

import * as number from "./number" //数字类
import * as testIf from "./testIf" //验证类
import * as verify from "./verify" //输入类
import * as operation from "./operation" //操作类
import * as array from "./array.js" //array校验
import * as string from "./string.js" //string 工具类
import * as define from "./define.js" //常用定义
import * as time from "./time.js"





/**
 * 去除空格
 */
function trim(str = "", type = "both") {
	return (str + "").replace({
		both: /^\s+|\s+$/g,
		left: /^\s*/,
		right: /(\s*$)/g,
		all: /\s+/g
	} [type], "")
}

/**
 * 深度拷贝
 */
function deepClone(obj1, obj2 = {}) {
	let toStr = Object.prototype.toString;
	let arrStr = toStr.call([]);
	for (let prop in obj1) {
		if (obj1.hasOwnProperty(prop)) {
			if (obj1[prop] !== null && typeof(obj1[prop]) == "object") {
				obj2[prop] = toStr.call(obj1[prop]) == arrStr ? [] : {};
				deepClone(obj1[prop], obj2[prop]);
			} else {
				obj2[prop] = obj1[prop];
			}
		}
	}
	return obj2;
}

/**
 * 获取URL参数
 */
function getUrlParam(url) {
	let arrObj = url.split("?");
	let params = Object.create(null)
	if (arrObj.length > 1) {
		arrObj = arrObj[1].split("&");
		arrObj.forEach(item => {
			item = item.split("=");
			params[item[0]] = item[1]
		})
	}
	return params;
}

/**
 * 获取随机id值(长度最高为10)
 */
function getRandomId(len = 10) {
	const id = Math.random().toString(36)
	return id.substr(3, len);
}


export default {
	...number, // 数字类函数
	...testIf, // 验证类函数
	...verify, // 输入类函数
	...operation, // 操作类函数
	...array,
	...string,
	...define, //常用定义
	...time, //时间日期


	trim, //去除空格
	deepClone, // 深度拷贝
	getUrlParam, // 获取url参数
	getRandomId, // 获取随机ID

}